package com.lxd.controller;

import java.util.List;

import com.lxd.entity.TokenUserInfoDto;
import com.lxd.entity.query.CommentQuery;
import com.lxd.entity.po.Comment;
import com.lxd.entity.vo.CommentVO;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.entity.vo.ResponseVO;
import com.lxd.redis.RedisComponent;
import com.lxd.service.CommentService;
import com.lxd.utils.Constants;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评论表 Controller
 */
@RestController("commentController")
@RequestMapping("/comment")
public class CommentController extends ABaseController{

	@Resource
	private CommentService commentService;
	@Resource
	private RedisComponent redis;
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList( CommentQuery query, HttpServletRequest request){
		String token = request.getHeader(Constants.HEADER_TOKEN);
		PaginationResultVO<CommentVO> listByPage = commentService.findListByPage(query);
		return getSuccessResponseVO(listByPage);
	}

	/**
	 * Admin 根据条件分页查询
	 */
	@RequestMapping("/admin/loadDataList")
	public ResponseVO AdminLoadDataList(CommentQuery query, HttpServletRequest request){
		String token = request.getHeader(Constants.HEADER_TOKEN);
		PaginationResultVO<CommentVO> listByPage = commentService.AdminFindListByPage(query);
		return getSuccessResponseVO(listByPage);
	}
	/**
	 * 点赞接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addLikeCount", method = RequestMethod.POST)
	public ResponseVO addLikeCount(HttpServletRequest request, @RequestBody Comment comment) {
		Long commentId = comment.getId();
		String token = request.getHeader(Constants.HEADER_TOKEN);
		TokenUserInfoDto tokenUserInfoDto = redis.getTokenUserInfoDto(token);
		String curLikeCount = redis.setLikeCount(tokenUserInfoDto.getUserId(), commentId.toString());
		commentService.updateLikeCountById(commentId,Long.valueOf(curLikeCount));
		return getSuccessResponseVO(curLikeCount);
	}


	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(@RequestBody Comment bean,HttpServletRequest request) {
		bean.setUserId(Long.valueOf(redis.getTokenUserInfoDto(request.getHeader(Constants.HEADER_TOKEN)).getUserId()));
		commentService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<Comment> listBean) {
		commentService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<Comment> listBean) {
		commentService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@RequestMapping("/getCommentById")
	public ResponseVO getCommentById(Long id) {
		return getSuccessResponseVO(commentService.getCommentById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@RequestMapping("/updateCommentById")
	public ResponseVO updateCommentById(@RequestBody CommentVO bean) {
		commentService.updateCommentById(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id删除
	 */
	@RequestMapping("/deleteCommentById")
	public ResponseVO deleteCommentById(Long id) {
		commentService.deleteCommentById(id);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据NewsIdAndActivityId查询对象
	 */
	@RequestMapping("/getCommentByNewsIdAndActivityId")
	public ResponseVO getCommentByNewsIdAndActivityId(Long newsId,Long activityId) {
		return getSuccessResponseVO(commentService.getCommentByNewsIdAndActivityId(newsId,activityId));
	}

	/**
	 * 根据NewsIdAndActivityId修改对象
	 */
	@RequestMapping("/updateCommentByNewsIdAndActivityId")
	public ResponseVO updateCommentByNewsIdAndActivityId(Comment bean,Long newsId,Long activityId) {
		commentService.updateCommentByNewsIdAndActivityId(bean,newsId,activityId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据NewsIdAndActivityId删除
	 */
	@RequestMapping("/deleteCommentByNewsIdAndActivityId")
	public ResponseVO deleteCommentByNewsIdAndActivityId(Long newsId,Long activityId) {
		commentService.deleteCommentByNewsIdAndActivityId(newsId,activityId);
		return getSuccessResponseVO(null);
	}



}